package com.lzs.esubscribe.filter;

import cn.hutool.json.JSONObject;
import com.lzs.esubscribe.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * token鉴权 过滤器
 *
 * @author : lzs
 * @Date : 2022-02-17
 * @Description :使用包含判断以处理get类型请求
 */
@Component
public class HaiyanFilter implements Filter {
    @Autowired
    JwtUtils jwtUtils;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        Map<String, String> map = new HashMap<>();
        String url = ((HttpServletRequest) servletRequest).getRequestURI();
        if (url != null) {
            // 注册/登录 请求直接放行
            // 拦截器取到请求先进行判断，如果是OPTIONS请求，则放行
            if ("OPTIONS".equals(((HttpServletRequest) servletRequest).getMethod().toUpperCase())) {
                System.out.println("Method:OPTIONS");
                filterChain.doFilter(servletRequest, servletResponse);
                return;
            }
            if (url.contains("/user/login") || url.contains("/user/regist") || url.contains("/user/checktoken")) {
                filterChain.doFilter(servletRequest, servletResponse);
                return;
            } else if (false) {
                // 其他请求验证token
                String token = ((HttpServletRequest) servletRequest).getHeader("token");
                if (token != null && token.length() > 0 && token.trim().length() > 0) {
                    //token验证结果
                    int verify = jwtUtils.verify(token);
                    if (verify != 1) {
                        //验证失败
                        if (verify == 2) {
                            map.put("errorMsg", "token已过期");
                        } else if (verify == 0) {
                            map.put("errorMsg", "用户信息验证失败");
                        } else if (verify == 3) {
                            map.put("errorMsg", "错误的token信息");
                        }
                    } else if (verify == 1) {
                        // 验证成功，放行
                        filterChain.doFilter(servletRequest, servletResponse);
                        return;
                    } else {
                        // token为空的返回
                        map.put("errorMsg", "未携带token信息");
                    }
                }
            } else {
                // 定义接口外的依赖接口放行
                filterChain.doFilter(servletRequest, servletResponse);
                return;
                // 放置在服务器端后关闭
                //map.put("errorMsg","禁止访问");
            }
            JSONObject jsonObject = new JSONObject(map);
            servletResponse.setContentType("application/json");
            //设置响应的编码
            servletResponse.setCharacterEncoding("utf-8");
            //响应
            PrintWriter pw = servletResponse.getWriter();
            pw.write(jsonObject.toString());
            pw.flush();
            pw.close();
        }
    }
}
